import { defHttp } from '/@/utils/http/axios';
import { PageResult } from '@/api/model/pageResult';
import { ContentTypeEnum } from '@/enums/httpEnum';
import { QueryUnitVo } from '@/api/base-data/unit/model/queryUnitVo';
import { QueryUnitBo } from '@/api/base-data/unit/model/queryUnitBo';
import { GetUnitBo } from '@/api/base-data/unit/model/getUnitBo';
import { CreateUnitVo } from '@/api/base-data/unit/model/createUnitVo';
import { UpdateUnitVo } from '@/api/base-data/unit/model/updateUnitVo';
import { UnitSelectorBo } from '@/api/base-data/unit/model/unitSelectorBo';

const baseUrl = '/basedata/unit';
const selectorBaseUrl = '/selector';
const region = 'cloud-api';

export function selector(params: QueryUnitVo): Promise<PageResult<UnitSelectorBo>> {
  return defHttp.get<PageResult<UnitSelectorBo>>(
    {
      url: selectorBaseUrl + '/unit',
      params,
    },
    {
      region,
    },
  );
}

export function loadUnit(ids: string[]): Promise<UnitSelectorBo[]> {
  return defHttp.post<UnitSelectorBo[]>(
    {
      url: selectorBaseUrl + '/unit/load',
      data: ids,
    },
    {
      contentType: ContentTypeEnum.JSON,
      region,
    },
  );
}

/**
 * 查询列表
 * @param params
 * @returns {Promise}
 */
export function query(params: QueryUnitVo): Promise<PageResult<QueryUnitBo>> {
  return defHttp.get<PageResult<QueryUnitBo>>(
    {
      url: baseUrl + '/query',
      params: params,
    },
    {
      region,
    },
  );
}

/**
 * 根据ID查询
 * @param id
 * @returns {Promise}
 */
export function get(id: String): Promise<GetUnitBo> {
  return defHttp.get<GetUnitBo>(
    {
      url: baseUrl,
      params: {
        id,
      },
    },
    {
      region,
    },
  );
}

/**
 * 新增
 * @param params
 * @returns {Promise}
 */
export function create(params: CreateUnitVo): Promise<void> {
  return defHttp.post<void>(
    {
      url: baseUrl,
      data: params,
    },
    {
      contentType: ContentTypeEnum.FORM_URLENCODED,
      region,
    },
  );
}

/**
 * 修改
 * @param params
 * @returns {Promise}
 */
export function update(params: UpdateUnitVo): Promise<void> {
  return defHttp.put<void>(
    {
      url: baseUrl,
      data: params,
    },
    {
      contentType: ContentTypeEnum.FORM_URLENCODED,
      region,
    },
  );
}
